# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=spglib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.0
pkgrel=3
pkgdesc="C library for finding and handling crystal symmetries (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://spglib.readthedocs.io/en/latest/'
msys2_repository_url="https://github.com/spglib/spglib"
msys2_references=(
  'archlinux: spglib'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: Python bindings")
if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-gcc-lbgfortran: Fortran Interface")
fi
source=("https://github.com/spglib/spglib/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-use-copy-instead-of-create_symlink.patch
        002-fix-pkgconfig-file.patch)
sha256sums=('b6026f5e85106c0c9ee57e54b9399890d0f29982e20e96ede0428b3efbe6b914'
            '02507aae9974741004fde0ba25ec66651b59197750a1766bc2be1340fd21b544'
            'eb764f1f605ca7aca02b193e68e073b4d9212d39056bfc597bd4219af78f99a2')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz ..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/001-use-copy-instead-of-create_symlink.patch
  patch -p1 -i "${srcdir}"/002-fix-pkgconfig-file.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      "${_extra_config[@]}" \
      -DSPGLIB_SHARED_LIBS=ON \
      -DSPGLIB_INSTALL=ON \
      -DSPGLIB_WITH_Fortran=ON \
      -DSPGLIB_WITH_Python=ON \
      -DSPGLIB_USE_OMP=ON \
      -DSPGLIB_WITH_TESTS=OFF \
      -DSPGLIB_COMPILATION_WARNING=OFF \
      -DPython_INSTALL_DIR=lib/python${_pyver}/site-packages \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
